package com.shuoyue.ycgk.ui.account;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.Nullable;

import com.shuoyue.ycgk.Constant;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.appcatch.MemeryCatch;
import com.shuoyue.ycgk.appcatch.SPUtils;
import com.shuoyue.ycgk.entity.UserInfo;
import com.shuoyue.ycgk.ui.account.login.WeChatLogin;
import com.shuoyue.ycgk.ui.common.CstWebActivity;
import com.shuoyue.ycgk.ui.main.MainTabActivity;
import com.shuoyue.ycgk.utils.XToast;

import butterknife.BindView;
import butterknife.OnClick;

public class AccountLoginActivity extends BaseLoginActivity implements AccountContract.View {

    @BindView(R.id.phone_num)
    EditText phoneNum;
    @BindView(R.id.password)
    EditText password;
    AccountContract.Presenter accountPresenter;
    static int srequestCode = -1;
    public final static int REQUEST_CODE = 1543;

    public static void start(Context context) {
        context.startActivity(new Intent(context, AccountLoginActivity.class));
    }


    public static void startForResult(Activity context) {
        context.startActivityForResult(new Intent(context, AccountLoginActivity.class).putExtra("srequestCode", REQUEST_CODE),
                REQUEST_CODE);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
//        UserInfo userInfo = SPUtils.getUserInfo(mContext);
//        if (userInfo != null) {
//            MemeryCatch.token = userInfo.getToken();
//            startActivity(new Intent(mContext, MainTabActivity.class));
//            finish();
//        }
        phoneNum.setText(SPUtils.getLoginUserName(mContext));
        password.setText(SPUtils.getLastLoginPwd(mContext));
    }

    @Override
    public void initData() {
        srequestCode = getIntent().getIntExtra("srequestCode", -1);
        accountPresenter = new AccountContract.Presenter();
        accountPresenter.attachView(this);
    }

    void loginCheck() {
        String phoneStr = phoneNum.getText().toString();
        String passwordStr = password.getText().toString();

        if (phoneStr.length() != 11) {
            XToast.toast(mContext, "请输入正确的手机号码");
            return;
        }
        if (passwordStr.length() < 6) {
            XToast.toast(mContext, "请输入正确的密码");
            return;
        }
        SPUtils.updataLoginUserName(mContext, phoneStr);
        SPUtils.updateUserPwd(mContext, passwordStr);
        accountPresenter.login(phoneStr, passwordStr, null, null);
    }

    @OnClick({R.id.clear, R.id.submit, R.id.go_regist
            , R.id.back, R.id.app_deal, R.id.smscode, R.id.wechat})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.clear:
                phoneNum.setText("");
                break;
            case R.id.submit:
                loginCheck();
                break;
            case R.id.go_regist:
                startActivity(new Intent(mContext, ResetPwdActivity.class));
                break;
            case R.id.back:
                finish();
                break;
            case R.id.app_deal:
                CstWebActivity.start(mContext,
                        Constant.HOST_INTERFACE
                                + "/api/share/agreement", "用户协议");
                break;

            case R.id.smscode:
                startActivity(new Intent(mContext, SMSLoginActivity.class));
                finish();
                break;
            case R.id.wechat:
                WeChatLogin.login(this, (openId, accesstoken) -> accountPresenter.wechatLogin(openId, accesstoken, null, null));
                break;
        }
    }

    @Override
    public void loginSuc(UserInfo userInfo) {
        if (userInfo != null) {
            MemeryCatch.token = userInfo.getToken();
            SPUtils.updateUserInfo(mContext, userInfo);
            if (srequestCode == -1) {
                startActivity(new Intent(mContext, MainTabActivity.class));
                finish();
            } else {
                setResult(RESULT_OK);
                finish();
            }
        }
    }

    @Override
    public void registSuc() {

    }

    @Override
    public void registFail(String msg) {

    }

    @Override
    public void resetSuc() {

    }

    @Override
    public void bindPhone(String openId, String accessToken) {
        BindPhoneActivity.start(this, openId, accessToken);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == BindPhoneActivity.REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                if (srequestCode == -1) {
                    startActivity(new Intent(mContext, MainTabActivity.class));
                    finish();
                } else {
                    setResult(RESULT_OK);
                    finish();
                }
            }
        }
    }
}
